package com.runmi.webchat.aftersale.web.api.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.common.utils.HttpUtil;
import com.runmi.webchat.aftersale.common.utils.MD5;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.config.WxConfig;
import com.runmi.webchat.aftersale.dbcore.dao.UserOpenIdDao;
import com.runmi.webchat.aftersale.dbcore.entity.ShopInfo;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.entity.UserOpenId;
import com.runmi.webchat.aftersale.dbcore.mapper.ShopInfoMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.UserOpenIdMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.web.api.vo.SetupReqVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/webchatapi/user")
public class WxUserController {
	
	@Autowired
	private UserOpenIdDao userOpenIdDao;
	
	@Autowired
	private UserOpenIdMapper userOpenIdMapper;
	
	 @Autowired
	 private ShopInfoMapper shopInfoMapper;
	
	
	@Autowired
	private WxConfig wxConfig;

	@ApiOperation(value = "获取用户信息,返回用户信息当 type:0：用户 1：店员'", notes = "获取用户信息",tags={ "微信API", })
	@RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
	@ResponseBody
	public UserOpenId getUserInfo(@ApiParam(value = "微信OPENID",required = true)@RequestParam(value = "openId") String openId){
		UserOpenId userInfo= userOpenIdDao.getUserByOpenId(openId);
		return userInfo;
	}
	
	@ApiOperation(value = "获取OPENID", notes = "获取OPENI",tags={ "微信API", })
	@RequestMapping(value = "/getOpenId",method = RequestMethod.GET)
	@ResponseBody
	public String getOpenId(@ApiParam(value = "code",required = true)@RequestParam(value = "code") String code){
		String url="https://api.weixin.qq.com/sns/jscode2session?appid="+wxConfig.getAppid()+"&secret="+wxConfig.getSecret()+"&js_code="+code+"&grant_type=authorization_code";
		return HttpUtil.doGetSSL(url, null);
		//return userInfo;
	}
	
	@ApiOperation(value = "店员密码验证'", notes = "店员密码验证'",tags={ "微信API", })
	@RequestMapping(value = "/checkPwd",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> checkPwd(
			@ApiParam(value = "店铺ID",required = true)@RequestParam(value = "shopId") Integer shopId,
			@ApiParam(value = "用户名",required = true)@RequestParam(value = "usrName") String usrName,
			@ApiParam(value = "IMA",required = true)@RequestParam(value = "pwd") String pwd){
		
		Map<String,Object> result=new HashMap<String,Object>();
		
		if(null==shopId || StringUtil.isBlank(usrName) || StringUtil.isBlank(pwd)){
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, "");					
			return result;
		}
		
		UserOpenId userOpenId =new UserOpenId();
		userOpenId.setShopId(shopId);
		userOpenId.setUsrName(usrName);
		userOpenId.setDelFlag((byte)0);
		List<UserOpenId> users=userOpenIdMapper.select(userOpenId);
		
		if(null==users || users.isEmpty()){
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, "用户名错误");	
			return result;
		}
		UserOpenId user= users.get(0);
		if(null==user){
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, "用户名错误");	
			return result;
		}
		
		if(null==user.getStatus() || user.getStatus()!=(byte)1 ){
			result.put(Constants.RETURN_CODE_KEY, "0300");
			result.put(Constants.RETURN_MESSAGE_KEY, "用户已被冻结");	
			return result;
		}
		
//		password="111111";
		String psdmd5=MD5.getMD5(pwd);
		if(!user.getPwd().equals(psdmd5)){		
			result.put(Constants.RETURN_CODE_KEY, "0200");
			result.put(Constants.RETURN_MESSAGE_KEY, "密码错误");	
			return result;
		}
		ShopInfo shopInfo= shopInfoMapper.selectByPrimaryKey(user.getShopId());
		
		result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
		result.put(Constants.RETURN_MESSAGE_KEY, Constants.RETURN_CODE_SUCCESS_MESSAGE);	
		result.put("user", user);
		result.put("shopName", shopInfo.getShopName());
		return result;
	}
	
//	@ApiOperation(value = "提交店员申请", notes = "获取用户信息",tags={ "微信API", })
//	@RequestMapping(value = "/saveShopUser",method = RequestMethod.GET)
//	@ResponseBody
//	public Result saveShopUser(
//			@ApiParam(value = "微信OPENID",required = true)@RequestParam(value = "openId") String openId,
//			@ApiParam(value = "门店ID",required = true)@RequestParam(value = "shopId") Integer shopId,
//			@ApiParam(value = "姓名",required = true)@RequestParam(value = "realName") String realName,
//			@ApiParam(value = "手机",required = true)@RequestParam(value = "mobile") String mobile){
//		
//		UserOpenId userInfo= userOpenIdDao.getUserInfoByOpenId(openId);
//		if(null==userInfo){
//			UserOpenId userOpenId=new UserOpenId();
//			userOpenId.setCreateTime(new Date());
//			userOpenId.setOpenId(openId);
//			userOpenId.setDelFlag((byte)0);
//			userOpenId.setMobile(mobile);
//			userOpenId.setRealName(realName);
//			userOpenId.setStatus((byte)0);
//			userOpenId.setShopId(shopId);
//			userOpenId.setUserType((byte)1);
//			userOpenIdMapper.insert(userOpenId);
//		}else{
//			if(userInfo.getStatus()==(byte)2){
//				return ResultFactory.getResultFail("店员已经冻结");
//			}
//			userInfo.setShopId(shopId);
//			userInfo.setMobile(mobile);
//			userInfo.setRealName(realName);
//			userInfo.setUserType((byte)1);
//			userOpenIdMapper.updateByPrimaryKey(userInfo);
//		}
//		return ResultFactory.getResultSuccess();
//	}
}
